package com.hsd.znsh.service.impl;

import com.hsd.znsh.entity.FarmType;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.repository.FarmTypeRepository;
import com.hsd.znsh.service.FarmTypeService;
import com.hsd.znsh.utils.BeanUtil;
import com.hsd.znsh.utils.HqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-26 15:18:07
 */
@Service
public class FarmTypeServiceImpl extends BaseServiceImpl implements FarmTypeService{

    @Autowired
    private FarmTypeRepository farmTypeRepository;

    @Override
    public long getFarmTypeListTotalPages(FarmType farmType) {
        return this.count(HqlUtil.getCountHql(farmType),farmType);
    }

    @Override
    public List<FarmType> getFarmTypeList(Pageable pageable,FarmType farmType) {
        List<FarmType> list=this.find(HqlUtil.getQueryHql(farmType),farmType,pageable);
        return list;
    }

    @Override
    public FarmType getFarmTypeInfo(Integer id) {
        FarmType farmType=farmTypeRepository.findOne(id);
        return farmType;
    }

    @Override
    public FarmType addFarmType(FarmType farmType) {
        return farmTypeRepository.save(farmType);
    }

    @Override
    public FarmType updateFarmType(FarmType farmType) {
        FarmType target=farmTypeRepository.findOne(farmType.getId());
        BeanUtil.copyPropertiesIgnoreNull(farmType,target);
        return farmTypeRepository.save(farmType);
    }

    @Override
    public void deleteFarmType(Integer id) {
        FarmType farmType=farmTypeRepository.findOne(id);
        farmType.setDeleted(DeletedStatusEnum.DELETED.getCode());
        farmTypeRepository.save(farmType);
    }

    @Override
    public List<FarmType> getFarmTypeList() {
        return farmTypeRepository.findByDeleted(DeletedStatusEnum.NOT_DELETED.getCode());
    }

}
